package com.bw.my.mvvm.view;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.bw.mvvm.BaseApplication;
import com.bw.mvvm.mvvm.view.BaseActivity;
import com.bw.my.BR;
import com.bw.my.R;
import com.bw.my.databinding.ActivityShipaddressBinding;
import com.bw.my.mvvm.viewmodel.ShipAddressViewModel;

public class ShipAddressActivity extends BaseActivity<ActivityShipaddressBinding, ShipAddressViewModel> {
    public Button NewAddress;
    private RelativeLayout background_rl;
    @Override
    public int bindLayout() {
        return R.layout.activity_shipaddress;
    }

    @Override
    public int initVerable() {
        return BR.shipaddress;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        NewAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setNewAddress();
            }
        });

    }

    public void initView(){
        NewAddress = findViewById(R.id.shipaddress_botton);
        background_rl = findViewById(R.id.background_rl);
    }

    public void setNewAddress(){
        background_rl.setVisibility(View.VISIBLE);
        PopupWindow popupWindow = new PopupWindow();
        View view = LayoutInflater.from(BaseApplication.getInstance())
                .inflate(R.layout.item_popupwindow,null);
        popupWindow.setContentView(view);
        popupWindow.setFocusable(true);
        popupWindow.setHeight(600);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(mView.getRoot(),0,-300);

        EditText editName = view.findViewById(R.id.edit_name);
        EditText editAddress = view.findViewById(R.id.edit_address);
        Button submit = view.findViewById(R.id.button_submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = editName.getText().toString();
                String address = editAddress.getText().toString();
                if (!name.isEmpty()){
                    if (!address.isEmpty()){
                        mViewModel.newAddress(name,address);
                        background_rl.setVisibility(View.GONE);
                        popupWindow.dismiss();

                    }
                }
            }
        });
        background_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                background_rl.setVisibility(View.GONE);
                popupWindow.dismiss();
            }
        });
    }

}
